#include "Stack.h"

void STInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;
	ps->_top = 0;
}

void STDestroy(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->_top = 0;
}

void STPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->capacity == ps->_top)
	{
		int size = ps->capacity == 0 ? 4 : ps->capacity*2;
		STDataType* tmp = (STDataType*)realloc(ps->a,sizeof(int)*size);
		if (tmp == NULL)
		{
			printf("realoc fila\n");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = size;
	}
	ps->a[ps->_top] = x;
	ps->_top++;
}

STDataType STPop(ST* ps)
{
	assert(ps);
	assert(ps->_top);
	STDataType tmp = ps->a[ps->_top - 1];
	ps->_top--;
	return tmp;
}

int STSize(ST* ps)
{
	assert(ps);
	return ps->_top;
}

bool STEmpty(ST* ps)
{
	assert(ps);
	return ps->_top == 0;
}


void STprint(ST* ps)
{
	for (int i = 0; i < ps->_top; i++)
	{
		printf("%d",ps->a[i]);
	}
	printf("\n");
}
